﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using RapidSalesAdminPortalAPI.DataAccess;
using RapidSalesAdminPortalAPI.Models;
using WebAPI.Common;
using static WebAPI.Models.Enums;

namespace RapidSalesAdminPortalAPI.Controllers
{
    /// <summary>
    /// H5-SalesPoint
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SalesPointFrontController : ParentFrontController
    {
        private readonly RS_AdmPortalContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        private readonly IDataProtectorUtility _dataProtectorUntility;

        public SalesPointFrontController(RS_AdmPortalContext context, IConfiguration configuration, ILogUtility logUtility, ILogger<ProductController> logger, IDataProtectorUtility dataProtectorUntility)
        {
            _context = context;
            _configuration = configuration;
            _logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            _dataProtectorUntility = dataProtectorUntility;
        }

        [HttpPost("CheckExclusive")]
        public async Task<ActionResult<ResultMsg>> CheckExclusive([FromBody] CheckExclusiveRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                CheckExclusiveResponse res = new CheckExclusiveResponse();
                res.IsNeedCode = false;
                IQueryable<FrIpSalesPoint> paComont = _context.FrIpSalesPoints.AsQueryable();
                paComont = paComont.Where(x => x.Enable == true);
                paComont = paComont.Where(x => x.Code == requestModel.SalesPointCode);
                var paData = paComont.ToList().FirstOrDefault();
                if (paData != null)
                {
                    var exclusive = paData.Exclusive.GetValueOrDefault();
                    if (exclusive)
                    {
                        res.IsNeedCode = true;
                    }
                }

                return resultMsg.SetResultMsg(
                (int)ResultCode.Success,
                HelperUtility.GetDescription(ResultCode.Success),
                res);
            }
            catch (Exception ex)
            {
                _logger.LogError("SalesPointFrontC/Check Error:" + ex.Message);
                return resultMsg.SetResultMsg(
                 (int)ResultCode.ErrorOnRequestUnMarshalling,
                 HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                 null);
            }
        }

        [HttpPost("CheckCode")]
        public async Task<ActionResult<ResultMsg>> CheckCode([FromBody] CheckCodeRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                CheckCodeResponse res = new CheckCodeResponse();
                res.IsOk = false;
                IQueryable<FrIpSalesPoint> paComont = _context.FrIpSalesPoints.AsQueryable();
                paComont = paComont.Where(x => x.Enable == true);
                paComont = paComont.Where(x => x.Code == requestModel.SalesPointCode);
                paComont = paComont.Where(x => x.Passcode == requestModel.Code);
                var paData = paComont.ToList().FirstOrDefault();
                if (paData != null)
                {
                    res.IsOk = true;
                }

                return resultMsg.SetResultMsg(
                (int)ResultCode.Success,
                HelperUtility.GetDescription(ResultCode.Success),
                res);
            }
            catch (Exception ex)
            {
                _logger.LogError("SalesPointFrontC/CheckCode Error:" + ex.Message);
                return resultMsg.SetResultMsg(
                 (int)ResultCode.ErrorOnRequestUnMarshalling,
                 HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                 null);
            }
        }


        [HttpPost("GetSalePointName")]
        public async Task<ActionResult<ResultMsg>> GetSalePointName([FromBody] GetSalePointNameRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                GetSalePointInfoResponse resModel = new GetSalePointInfoResponse();
                IQueryable<FrIpSalesPoint> paComont = _context.FrIpSalesPoints.AsQueryable();
                paComont = paComont.Where(x => x.Enable == true);
                paComont = paComont.Where(x => x.Code == requestModel.SalesPointCode);
                var paData = paComont.ToList().FirstOrDefault();
                if (paData != null)
                {
                    resModel.ID = paData.Id.ToString();
                    switch (requestModel.lang)
                    {
                        case (int)LanguageType.SC:
                            resModel.Name = paData.NameSc;
                            break;
                        case (int)LanguageType.TC:
                            resModel.Name = paData.NameTc;
                            break;
                        case (int)LanguageType.OT:

                            break;
                        case (int)LanguageType.En:
                        default:
                            resModel.Name = paData.NameEn;
                            break;
                    }
                }

                //query Max Qty per Transaction
                int maxqty = 0;
                IQueryable<FrIpMasterDatum> md = _context.FrIpMasterData.AsQueryable();
                md = md.Where(x => x.Enable == true);
                md = md.Where(x => x.Code == "QLPT");
                var m = md.ToList().FirstOrDefault();
                if (m != null) {
                    int.TryParse(m.Value, out maxqty);
                }
                resModel.maxQty = maxqty;

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     resModel);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     ex.Message);
            }
        }

    }
}
